package com.lateam2010.send.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Send extends Composite {

	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	
	private static SendUiBinder uiBinder = GWT.create(SendUiBinder.class);

	interface SendUiBinder extends UiBinder<Widget, Send> {
	}

	@UiField CheckBox sendToParents;
	@UiField Button send;
	@UiField TextBox from;
	@UiField TextBox subject;
	@UiField TextBox password;
	@UiField TextArea body;
	@UiField Label sending;
	@UiField Label sent;
	@UiField Label error;
	@UiField Label invalidPassword;
	@UiField Panel passwordPanel;

	public Send() {
		initWidget(uiBinder.createAndBindUi(this));
		sending.setVisible(false);
		sent.setVisible(false);
		error.setVisible(false);
		invalidPassword.setVisible(false);
		passwordPanel.setVisible(false);
		String f = Cookies.getCookie("from");
		if (f != null) {
			from.setText(f);
		}
		String p = Cookies.getCookie("password");
		if (p != null) {
			password.setText(f);
		}
	}
	
	@UiHandler("sendToParents")
	void onBlur(ClickEvent e) {
		validate();
	}
	@UiHandler("sendToParents")
	void onUnblur(ClickEvent e) {
		validate();
	}
	private void validate() {
		passwordPanel.setVisible(sendToParents.getValue());
	}

	@UiHandler("send")
	void onClick(ClickEvent e) {
		subject.setEnabled(false);
		body.setEnabled(false);
		from.setEnabled(false);
		sendToParents.setEnabled(false);
		send.setVisible(false);
		sending.setVisible(true);
		error.setVisible(false);
		greetingService.greetServer(password.getText(), sendToParents.getValue(), from.getText(), subject.getText(), body.getText(), new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				if (result.booleanValue()) {
					Cookies.setCookie("from", from.getText());
					Cookies.setCookie("password", password.getText());
					sending.setVisible(false);
					sent.setVisible(true);
				} else {
					subject.setEnabled(true);
					body.setEnabled(true);
					from.setEnabled(true);
					sendToParents.setEnabled(true);
					sending.setVisible(false);
					send.setVisible(true);
					invalidPassword.setVisible(true);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				subject.setEnabled(true);
				body.setEnabled(true);
				from.setEnabled(true);
				sendToParents.setEnabled(true);
				sending.setVisible(false);
				send.setVisible(true);
				error.setVisible(true);
			}
		});
	}

}
